<template>
  <el-dialog 
    :visible.sync="visible" 
    :title="!dataForm.id ? '添加':'修改'"
    :close-on-click-modal="false" 
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  @keydown.enter.native.prevent="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="dictName" label="字典名称">
        <el-input v-model="dataForm.dictName" placeholder="字典名称"></el-input>
      </el-form-item>
      <el-form-item prop="dictType" label="字典类型">
        <el-input v-model="dataForm.dictType" placeholder="字典类型"></el-input>
      </el-form-item>
      <!-- <el-form-item prop="sort" label="排序">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序"></el-input-number>
      </el-form-item> -->
      <el-form-item prop="remark" label="备注">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import servicesApi from '@/services/servicesApi'
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        dictName: '',
        dictType: '',
        sort: 0,
        remark: ''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        dictName: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ],
        dictType: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id =id || 0
      // console.log('dictTypeId=%s', id)
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      servicesApi.getDictInfoById(this.dataForm.id).then(res => {
        if (res.data.code !== 200) {
          return this.$message.error(res.data.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data.data
        }
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.dataForm.id) { // 修改
          // console.log('修改字典')
          servicesApi.updateDict(this.dataForm).then(res => {
            if (res.data.code !== 200) {
              return this.$message.error(res.data.msg)
            }
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        } else {  // 新增
          // console.log('新增字典')
          servicesApi.addDict(this.dataForm).then(res => {
            if (res.data.code !== 200) {
              return this.$message.error(res.data.msg)
            }
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
          })
          })
        }
        // this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/dict/type', this.dataForm).then(({ data: res }) => {
        //   if (res.code !== 0) {
        //     return this.$message.error(res.msg)
        //   }
        //   this.$message({
        //     message: this.$t('prompt.success'),
        //     type: 'success',
        //     duration: 500,
        //     onClose: () => {
        //       this.visible = false
        //       this.$emit('refreshDataList')
        //     }
        //   })
        // }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
